package app.util;

import java.io.File;
import java.lang.management.ManagementFactory;

import org.eclipse.jetty.jmx.MBeanContainer;
import org.eclipse.jetty.security.HashLoginService;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AllowSymLinkAliasChecker;
import org.eclipse.jetty.webapp.WebAppContext;
/**
*<dependency>
*<groupId>org.eclipse.jetty</groupId>
*<artifactId>jetty-webapp</artifactId>
*<version>${project.version}</version>
*</dependency>
*/
public class OneWebApp {

	public static void main(String[] args) throws Exception {
		Server server = new Server(8083);
		
		//JMX在Java编程语言中定义了应用程序以及网络管理和监控的体系结构、设计模式、应用程序接口以及服务。通常使用JMX来监控系统的运行状态或管理系统的某些方面，比如清空缓存、重新加载配置文件等
		MBeanContainer mBeanContainer = new MBeanContainer(
				ManagementFactory.getPlatformMBeanServer());
		server.addBean(mBeanContainer);
		
		WebAppContext webapp = new WebAppContext();
		webapp.setContextPath("/");
		File warFile = new File("src/main/resources/" + "test.war");
		webapp.setWar(warFile.getAbsolutePath());
		webapp.addAliasCheck(new AllowSymLinkAliasChecker());
		
		/*<security-constraint>
		    <web-resource-collection>
		      <web-resource-name>A Protected Page</web-resource-name>
		      <url-pattern></url-pattern>
		    </web-resource-collection>
		    <auth-constraint>
		      <role-name>admin</role-name>
		      <role-name>user</role-name>
		      <role-name>moderator</role-name>
		    </auth-constraint>
		  </security-constraint>
		  <login-config>
		    <auth-method>BASIC</auth-method>
		    <realm-name>MyRealm</realm-name>
		  </login-config>*/
		//web程序中配置了以上跟登录相关的，需要加上以下代码才可以启动，否则会报NoLoginService错误。或者给web程序加个jetty.xml
	    HashLoginService dummyLoginService = new HashLoginService("TEST-SECURITY-REALM");   
	    webapp.getSecurityHandler().setLoginService(dummyLoginService);
	    
		server.setHandler(webapp);
		
		server.start();
		server.join();
		
	}
}
